package com.bizteco.ishark;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Iterator;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.TableTreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableTree;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

public class DriverDialog extends Dialog {
	private Text text;
	private Text text_2;
	private Text text_3;
	private Text text_4;
	private Text text_5;
	private Button bt_AddFile;
	public ListViewer listViewer;

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public DriverDialog(Shell parentShell) {
		super(parentShell);
		setShellStyle(SWT.CLOSE | SWT.MIN | SWT.MAX | SWT.RESIZE);
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(4, false));
		
		Label label_DriverName = new Label(container, SWT.NONE);
		label_DriverName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_DriverName.setText("Driver Name");
		
		text = new Text(container, SWT.BORDER);
		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblCatalog = new Label(container, SWT.NONE);
		lblCatalog.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		lblCatalog.setText("Catalog");
		
		Combo combo_1 = new Combo(container, SWT.NONE);
		combo_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblDescription = new Label(container, SWT.NONE);
		lblDescription.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		lblDescription.setText("Description");
		
		text_2 = new Text(container, SWT.BORDER);
		text_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		
		Label lblClassName = new Label(container, SWT.NONE);
		lblClassName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		lblClassName.setText("Class Name");
		
		text_3 = new Text(container, SWT.BORDER);
		text_3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		
		Label lblSampleUrl = new Label(container, SWT.NONE);
		lblSampleUrl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		lblSampleUrl.setText("Sample URL");
		
		text_4 = new Text(container, SWT.BORDER);
		text_4.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		
		Label lblDefaultPort = new Label(container, SWT.NONE);
		lblDefaultPort.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		lblDefaultPort.setText("Default Port");
		
		text_5 = new Text(container, SWT.BORDER);
		text_5.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		
		TabFolder tabFolder = new TabFolder(container, SWT.NONE);
		GridData gd_tabFolder = new GridData(SWT.LEFT, SWT.CENTER, true, false, 4, 1);
		gd_tabFolder.widthHint = 452;
		gd_tabFolder.heightHint = 267;
		tabFolder.setLayoutData(gd_tabFolder);
		
		TabItem cItem_Libraries = new TabItem(tabFolder, SWT.NONE);
		cItem_Libraries.setText("Libraries");
		
		Composite composite = new Composite(tabFolder, SWT.NONE);
		cItem_Libraries.setControl(composite);
		composite.setLayout(new GridLayout(2, false));
		
		listViewer = new ListViewer(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		org.eclipse.swt.widgets.List list = listViewer.getList();
		GridData gd_list = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 6);
		gd_list.widthHint = 336;
		list.setLayoutData(gd_list);
		
		bt_AddFile = new Button(composite, SWT.CENTER);
		bt_AddFile.addSelectionListener( new AddFile());
		bt_AddFile.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		bt_AddFile.setText("Add File");
		
		Button bt_AddFolder = new Button(composite, SWT.CENTER);
		bt_AddFolder.addSelectionListener( new AddDirectory());
		bt_AddFolder.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		bt_AddFolder.setText("Add Folder");
		
		Button bt_Delete = new Button(composite, SWT.NONE);
		bt_Delete.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		bt_Delete.setText("Delete");
		
		Button bt_Up = new Button(composite, SWT.NONE);
		bt_Up.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		bt_Up.setText("Up");
		
		Button bt_Down = new Button(composite, SWT.NONE);
		bt_Down.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		bt_Down.setText("Down");
		
		Button bt_Classpath = new Button(composite, SWT.NONE);
		bt_Classpath.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		bt_Classpath.setText("Classpath");
		
		TabItem cItem_Properties = new TabItem(tabFolder, SWT.NONE);
		cItem_Properties.setText("Connection Properties");
		
		TableTreeViewer tableTreeViewer = new TableTreeViewer(tabFolder, SWT.BORDER | SWT.FULL_SELECTION);
		TableTree tableTree = tableTreeViewer.getTableTree();
		cItem_Properties.setControl(tableTree);

		TabItem cItem_Prameters = new TabItem(tabFolder, SWT.NONE);
		cItem_Prameters.setText("Adv. parameters");

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		Button button = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
//				System.out.println(" Click");
				
			}
		});
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(486, 440);
	}
	
	public void loadJar( File jars[]) {
		
		try {
			final URLClassLoader loader = (URLClassLoader)ClassLoader.getSystemClassLoader();
			final Method method = URLClassLoader.class.getDeclaredMethod( "addURL", new Class[]{ URL.class});
			
			method.setAccessible( true);
			
			for (File jar : jars) {
				method.invoke( loader, new Object[]{jar.toURI().toURL()});
				System.out.println( jar.getName() + " is loaded.");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	class AddFile implements SelectionListener {
	    
		public void widgetSelected(SelectionEvent event) {
			FileDialog fd = new FileDialog( Display.getDefault().getActiveShell(), SWT.OPEN);
			fd.setText("Open driver library");
			fd.setFilterPath("C:/");
			String[] filterExt = { "*.jar", "*.*" };
			fd.setFilterExtensions( filterExt);
			String selectedFileName = fd.open();
	      
			if( selectedFileName == null) // Cancel
				return;
	      
			listViewer.add( selectedFileName);
	    }

	    public void widgetDefaultSelected( SelectionEvent event) {}
	 
	}
	
	class AddDirectory implements SelectionListener {
	    
		public void widgetSelected(SelectionEvent event) {
			org.eclipse.swt.widgets.DirectoryDialog directoryDaialog = new org.eclipse.swt.widgets.DirectoryDialog( Display.getDefault().getActiveShell(), SWT.OPEN);
			directoryDaialog.setText("Open driver directory");
			directoryDaialog.setFilterPath("C:/");
			String selectedDirectoryName = directoryDaialog.open();

			if( selectedDirectoryName == null) // Cancel
				return;
	      
			listViewer.add( selectedDirectoryName);
			
	    }

	    public void widgetDefaultSelected( SelectionEvent event) {}
	 
	}
	

	
}


